#!/bin/bash

# 这个脚本用于设置ROS2项目使用系统Python 3.10

# 检查当前环境
echo "检查当前Python环境..."
echo "当前PATH: $PATH"
echo "当前Python: $(which python3) ($(python3 --version))"

# 如果检测到pyenv，则从PATH中移除
if [[ $PATH == *".pyenv/shims"* ]]; then
  echo "检测到pyenv，正在从PATH中移除..."
  export PATH=$(echo $PATH | sed -e 's|/home/felix/.pyenv/shims:||')
  echo "已修改PATH: $PATH"
else
  echo "未检测到pyenv，无需修改PATH"
fi

# 确认当前使用的是系统Python 3.10
echo "当前使用的Python: $(which python3) ($(python3 --version))"

# 检查empy模块是否已安装
if python3 -c "import em" &> /dev/null; then
  echo "empy模块已安装"
else
  echo "安装empy模块..."
  pip3 install empy
fi

# 创建一个用于ROS2开发的.bashrc配置
cat > ~/learn_ros2/utils/ros2_python_env.sh << 'EOF'
# ROS2 Python环境配置
# 将此文件添加到您的.bashrc中: source ~/learn_ros2/utils/ros2_python_env.sh

# 如果检测到pyenv，则从PATH中移除
if [[ $PATH == *".pyenv/shims"* ]]; then
  export PATH=$(echo $PATH | sed -e 's|/home/felix/.pyenv/shims:||')
fi

# 确认使用的是系统Python
echo "ROS2开发环境已启用，使用Python: $(which python3) ($(python3 --version))"

# 设置ROS2环境变量
source /opt/ros/humble/setup.bash
EOF

echo "已创建ROS2 Python环境配置文件: ~/learn_ros2/utils/ros2_python_env.sh"
echo "您可以通过以下命令将其添加到.bashrc中:"
echo "echo 'source ~/learn_ros2/utils/ros2_python_env.sh' >> ~/.bashrc"
echo ""
echo "或者每次开发前手动执行:"
echo "source ~/learn_ros2/utils/ros2_python_env.sh"

# 使配置文件可执行
chmod +x ~/learn_ros2/utils/ros2_python_env.sh

echo ""
echo "配置完成。现在您可以使用系统Python 3.10进行ROS2开发。" 